<?php
error_reporting(0);
session_start();
include_once '../comm/config.php';
include_once '../comm/checkpostandget.php';
include_once 'check.php';
include_once 'page.class.php';
include_once '../comm/wjt.php';

if($_POST['do']=='del'){
    $ids=$_POST["delid"];
	$sql="delete from ".$BIAOTOU."msg where Id=".$ids;
	mysql_query($sql);
	echo "0";
	exit();
}

$recuser=$_GET['senduser'];
$recid=$_GET["redid"];
//$outuser=$_POST['ddusername']; 
$content1=$_POST['content']; 
$act=$_GET['act'];
$page = !($_GET['page'])?'1':intval($_GET['page']);
$pagesize=10;
$page2=($page-1)*$pagesize;

if (!$act and !$outuser) {
	echo "缺少必要参数";
	exit;
}
else
{
	if($act=='in'){
		$sql="select * from ".$BIAOTOU."msg where ddusername='".$_SESSION["duoduouser"]."' or senduser='".$_SESSION["duoduouser"]."'";
		$rs = mysql_query($sql);
		$total = mysql_num_rows($rs);	
		
		$sql="select * from ".$BIAOTOU."msg where ddusername='".$_SESSION["duoduouser"]."' or senduser='".$_SESSION["duoduouser"]."' order by id desc limit $page2,$pagesize";
		$rs = mysql_query($sql);
	}
	/*if($act=='out'){
		$sql="select * from ".$BIAOTOU."msg where senduser='".$_SESSION["duoduouser"]."'";
		$rs = mysql_query($sql);
		$total = mysql_num_rows($rs);	
		
		$sql="select * from ".$BIAOTOU."msg where senduser='".$_SESSION["duoduouser"]."' order by id desc limit $page2,$pagesize";
		$rs = mysql_query($sql);
	}*/
    
	//阅读信息
    if($act=='send'&&$recid>0)
	{
		$sql="update ".$BIAOTOU."msg set see=1 where Id=".$recid;
		$res=mysql_query($sql);
	}
	//发送短信
	if($act=='send'&&$content1!=''){
		if($_SESSION["duoduouser"]!='网站客服')
		{
			$sj= date("Y-m-d H:i:s");
			$q="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('站内消息','".$content1."','".$sj."',0,'网站客服','".$_SESSION["duoduouser"]."')";
			$rs = mysql_query($q);
		}
		echo "<script language=javascript>window.location='msg_list.php?act=in';</script>";
	}
}

$tag="center";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>短消息 - 用户中心</title>

<link href="../template/<?=MOBAN?>/css/style.css" rel="stylesheet" type="text/css" />
<link  href="../template/<?=MOBAN?>/css/layout.css" rel="stylesheet" type="text/css" />
<link  href="../template/<?=MOBAN?>/css/comm.css" rel="stylesheet" type="text/css" />
<link  href="../template/<?=MOBAN?>/css/u_msg.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../js/jquery.js"></script>
<script language="javascript" type="text/javascript">
function getLength(str) {
    var len = str.length;
    var reLen = 0;
    for (var i = 0; i < len; i++) {        
        if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
            // 全角    
            reLen += 2;
        } else {
            reLen++;
        }
    }
    return reLen;    
}
function checkoutuser(outuser)
{
    	if(getLength(outuser)==0)
		{
			document.getElementById("ckddusername").innerHTML = '<img src=../images/cuo.gif alt="错误" align=absmiddle /> <font color=#ff0000>请输入消息接收人</font>';
		}
		else
		{
			document.getElementById("ckddusername").innerHTML = '<img src=../images/dui.gif alt="正确" align=absmiddle /> <font color=#009900></font>';
		}
}
function checkcontent(content)
{
    	if(getLength(content)==0)
		{
			document.getElementById("ckcontent").innerHTML = '<img src=../images/cuo.gif alt="错误" align=absmiddle /> <font color=#ff0000>请输入短信内容</font>';
		}
		else
		{
			document.getElementById("ckcontent").innerHTML = '<img src=../images/dui.gif alt="正确" align=absmiddle /> <font color=#009900></font>';
		}
}
function checkall()
{
	if(document.getElementById("ddusername").value==""||document.getElementById("content").value=="")
	{
		alert('请填写完内容！');
		return false;
	}
	else
	{
		return true;
	}
}

function selAll(obj){
    $(obj).attr("checked",'true');//全选
}
function selNone(obj){
    $(obj).removeAttr("checked");//取消全选
}
function selfan(obj){
    $(obj).each(function(){
		if($(this).attr("checked")){
			$(this).removeAttr("checked");
		}
		else{
		    $(this).attr("checked",'true');
		}
	})
}

/*接受到的*/
function MouseEnter(id)
{
	var div1=document.getElementById("jtid_"+id);
	var div2=document.getElementById("note_"+id);
	var div3=document.getElementById("ctent_"+id);
	var div4=document.getElementById("close_"+id);
	
	if(div1.className!="jt1")
	{
	  div1.className="jt1";
	  div2.className="note1";
	  div3.className="content1";
	  div4.style.display="block";	  
	}
}
function MouseLeave(id)
{
	var div1=document.getElementById("jtid_"+id);
	var div2=document.getElementById("note_"+id);
	var div3=document.getElementById("ctent_"+id);
	var div4=document.getElementById("close_"+id);

	if(div1.className!="jt")
	{
	  div1.className="jt";
	  div2.className="note";
	  div3.className="content";	
	  div4.style.display="none";  
	}	
}

/*我发送的*/
function MouseEnter1(id)
{
	var div1=document.getElementById("jtid_"+id);
	var div2=document.getElementById("note_"+id);
	var div3=document.getElementById("ctent_"+id);
	var div4=document.getElementById("close_"+id);
	
	if(div1.className!="sjt1")
	{
	  div1.className="sjt1";
	  div2.className="snote1";
	  div3.className="scontent1";
	  div4.style.display="block";	  
	}
}
function MouseLeave1(id)
{
	var div1=document.getElementById("jtid_"+id);
	var div2=document.getElementById("note_"+id);
	var div3=document.getElementById("ctent_"+id);
	var div4=document.getElementById("close_"+id);

	if(div1.className!="sjt")
	{
	  div1.className="sjt";
	  div2.className="snote";
	  div3.className="scontent";	
	  div4.style.display="none";  
	}	
}

function SendMsg(senduser,id)
{
	var url="msg_list.php?act=send&senduser="+senduser+"&redid="+id;
	window.location=url;
}

function Delmsg(id)
{
  if(confirm('您确定要删除吗?'))	
  {
	  $.ajax({
		  type:"POST",
		  url:"msg_list.php",
		  data:"do=del&act=in&delid="+id,
		  success:function(msg)
		  {
			 if(msg=='0')
			 {
				 window.location="msg_list.php?act=in";
			 }
		  }
		  });
  }
}
</script>
</head>
<body>
<?php
$modtag="msg";
include("../template/".MOBAN."/header_user.tpl.php");
?>
<div class="msgfm" style="margin-top:0px">

  <div class="uInfoTop">
     <div class="break"><a href="javascript:void();" onclick="javascript:window.history.back();"><img src="../template/<?=MOBAN?>/images/back.gif" width="29" height="30" border="0" /></a></div>
     <div class="username usernamefont"><?=$_SESSION["duoduouser"]?></div>
     <div class="userstate"><a class="ustatefont" href="exit.php">退出</a></div>
  </div>
  <div class="FRight" style="float:left;margin-top:10px"><a href="msg_list.php?act=send" class="btn" style="height:10px;line-height:10px">发消息给客服</a><a href="msg_list.php?act=send" class="btn"  style="height:10px;line-height:10px">发帖咨询优优助手</a></div>
   <div class="usertopline"></div>
   <?php if($act=='in'){?>
   <!--消息开始-->
   <?php 
	 $i=1;
	 while ($row = mysql_fetch_array($rs)){
   ?>
   <?php if($row["senduser"]!=$_SESSION["duoduouser"]){?>
  <div class="item">
     <div class="head"><img src="../template/<?=MOBAN?>/images/infoTXT.gif" width="73" height="73" border="0" /></div>
     <div class="top">
        <div class="t font1">客服</div>
        <div class="t font2" style="margin-left:10px;display:inline"><?=$row["addtime"]?></div>
        <?php if($row["see"]=="1"){?>
        <div class="t msgico"><img src="../images/msg-read.gif" border="0" /></div>
        <?php }else{?>
        <div class="t msgico"><img src="../images/msg.gif" border="0" /></div>
        <?php }?>
      </div>
      <div class="msgbox" onmousemove="MouseEnter(<?=$i?>);" onmouseout="MouseLeave(<?=$i?>);" onclick="SendMsg('<?=urlencode($row["senduser"])?>',<?=$row["Id"]?>)">
        <a onclick="return confirm('您确定要删除吗?')" href="msg_del.php?id=<?=$row["Id"]?>"><div class="close" id="close_<?=$i?>"></div></a>
      	<div class="jt" id="jtid_<?=$i?>"></div>
        <div class="note" id="note_<?=$i?>"><div class="content" id="ctent_<?=$i?>"><?=$row["content"]?></div></div>
      </div>
  </div>
  <?php }else{//以下代码是显示发送的?> 
  <div class="item">
       <div class="shead"><img src="../upload/avatar/<?=get_avatar_filepath($_SESSION['dduserid'], $size = 'big')?>" width="73" height="73" border="0" /></div>
       <div class="stop">
          <div class="st font1"><?=$row["senduser"]?></div>
          <div class="st font2" style="margin-right:10px;display:inline"><?=$row["addtime"]?></div>
          <?php if($row["see"]=="1"){?>
          <div class="st smsgico"><img src="../images/msg-read.gif" border="0" /></div>
          <?php }else{?>
          <div class="st smsgico"><img src="../images/msg.gif" border="0" /></div>
          <?php }?>
       </div>
       <div class="smsgbox" onmousemove="MouseEnter1(<?=$i?>);" onmouseout="MouseLeave1(<?=$i?>);">
          <a onclick="Delmsg(<?=$row["Id"]?>);" href="javascript:void(0)"><div class="sclose" id="close_<?=$i?>"></div></a>
          <div class="sjt" id="jtid_<?=$i?>"></div>
          <div class="snote" id="note_<?=$i?>"><div class="scontent" id="ctent_<?=$i?>"><?=$row["content"]?></div></div>
       </div>
  </div>
  <?php }?>
  <?php $i++;}?>
    <div class="megas512" style="clear:both"><?php pageft($total,$pagesize,1,1,0,10,"msg_list.php?act=".$act."");	echo $pagenav;?></div>   
  <?php }?> 
  
  <!--显示发送模块--> 
  <?php if($act=='send'){?>
    <FORM onsubmit="return checkall()" method=post action="msg_list.php?act=send">
    <INPUT type="hidden" id="ddusername" name="ddusername" value="网站客服" />
  	<div class="line" style="width:800px;margin-left:20px;margin-top:50px;display:inline">收信人：<font class="font3">优米库客服</font></div>
  	<div class="line" style="width:800px;margin-left:20px;margin-top:10px;height:auto;display:inline">
        <div class="cp">内&nbsp;&nbsp;&nbsp;&nbsp;容：</div>
        <div class="memo">
          <textarea id="content" name="content" onblur="checkcontent(document.getElementById('content').value);" style="width:585px;height:190px;border:0;overflow:hidden" ></textarea>
        </div>
    </div>  
    <div class="line" style="height:56px;margin-top:50px">
     <input type="submit" value="" class="bsendbutton" style="margin-left:73px;display:inline" />
    </div>
    </FORM>
  <?php }?>
</div>

<div class="lineclear" style="margin-top:50px"></div>
<?php
include("../template/".MOBAN."/footer.tpl.php");
mysql_close();
?>
</body>
</html>